package com.micro.modual.service.impl;

import com.micro.common.MicroConstant;
import com.micro.common.MicroResponseCode;
import com.micro.common.biz.GeneratorCodeUtil;
import com.micro.common.util.DateUtil;
import com.micro.core.exception.BizException;
import com.micro.core.frame.service.impl.AbstractBasicsServiceImpl;
import com.micro.core.resp.RespJson;
import com.micro.modual.entity.Seckill;
import com.micro.modual.entity.SeckillActivitySence;
import com.micro.modual.extension.SeckillExtension;
import com.micro.modual.mapper.SeckillActivitySenceMapper;
import com.micro.modual.mapper.SeckillMapper;
import com.micro.modual.model.bo.seckill.SeckillBO;
import com.micro.modual.model.bo.seckill.SeckillStatusBO;
import com.micro.modual.service.SeckillService;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.List;


/**
*  <h1>秒杀表  实现</h1>
 * @author LevelCoder
 * @version V1.0.0
 * @email LevelCoder@126.com
 * @date 2020-08-04 00:11:10
 */
@Service("seckillService")
public class SeckillServiceImpl extends AbstractBasicsServiceImpl<Seckill,Long> implements SeckillService {

    @Autowired
    private SeckillMapper seckillMapper;

    @Autowired
    private SeckillActivitySenceMapper seckillActivitySenceMapper;
    
    /**
     * <h2>新增秒杀表详情</h2>
     * @param seckillBO {@link SeckillBO} 秒杀表 新增/修改 请求对象
     * @return {@link RespJson} 返回结果基础类
     */
    @Override
    public RespJson addSeckill(SeckillBO seckillBO) {
        String seckillCode = GeneratorCodeUtil.generatorSystemCode();
        Seckill seckill = SeckillExtension.converAddSeckill(seckillBO,seckillCode);
        seckillMapper.insertSelective(seckill);
        if(CollectionUtils.isNotEmpty(seckillBO.getSceneCodeList())){
            List<SeckillActivitySence> seckillActivitySenceList = SeckillExtension.convertAddSeckillActivitySence(seckillCode,seckillBO.getSceneCodeList());
            seckillActivitySenceMapper.insertListAllCols(seckillActivitySenceList);
        }
        return new RespJson(MicroResponseCode.SUCCESS.getResponseCode(), MicroResponseCode.SUCCESS.getResponseMessage());
    }

    /**
     * <h2>修改秒杀表详情</h2>
     * @param seckillBO  {@link SeckillBO} 秒杀表 新增/修改 请求对象
     * @return {@link RespJson} 返回结果基础类
     */
    @Override
    public RespJson editSeckill(SeckillBO seckillBO) {
        Seckill seckill = SeckillExtension.converEditSeckill(seckillBO);
        seckillMapper.updateByPrimaryKeySelective(seckill);
        return new RespJson(MicroResponseCode.SUCCESS.getResponseCode(),MicroResponseCode.SUCCESS.getResponseMessage());

    }

    /**
     * <h2>根据主键编号删除秒杀表信息</h2>
     * @param id  秒杀表 主键编号
     * @return  {@link RespJson} 返回结果基础类
     */

    @Override
    public RespJson deleteSeckill(Long id) {
        seckillMapper.deleteByPrimaryKey(id);
        return new RespJson(MicroResponseCode.SUCCESS.getResponseCode(),MicroResponseCode.SUCCESS.getResponseMessage());
    }

    @Override
    public RespJson modifySeckillOnlineStatus(SeckillStatusBO seckillStatusBO) throws ParseException {
        Seckill seckill = seckillMapper.selectByPrimaryKey(seckillStatusBO.getSeckillId());
        //如果更改为上线状态
        if(seckillStatusBO.getSeckillStatus().equals(MicroConstant.StatusEffective.ENABLE)){
            String nowDate = DateUtil.getToday();
            DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
            if(seckill.getStartDate().compareTo(dateFormat.parse(nowDate)) < 1){
                throw new BizException(MicroResponseCode.START_TIME_MUST_GREATER_THAN_NOW_TIME.getResponseCode(),MicroResponseCode.START_TIME_MUST_GREATER_THAN_NOW_TIME.getResponseMessage());
            }
        }
        if(seckill.getSeckillStatus().equals(MicroConstant.StatusExecution.IN_PROGRESS)){
            throw new BizException(MicroResponseCode.HAS_IN_PROGRESS_SECKILL_NOT_ALLOW_CLOSE.getResponseCode(),MicroResponseCode.HAS_IN_PROGRESS_SECKILL_NOT_ALLOW_CLOSE.getResponseMessage());
        }
        seckill.setOnlineStatus(seckillStatusBO.getSeckillStatus());
        seckillMapper.updateByPrimaryKeySelective(seckill);
        return new RespJson(MicroResponseCode.SUCCESS.getResponseCode(),MicroResponseCode.SUCCESS.getResponseMessage());
    }

}
